/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dalfactory;

import dalInterface.DALFactory;
import dalInterface.DALAccount;
import dalInterface.DALMessage;
import dalInterface.DALQuerry;
import dalInterface.DALRelation;
import dalInterface.DALRoom;
import dalobject.DALAccountImpl;
import dalobject.DALMessageImpl;
import dalobject.DALQuerryImpl;
import dalobject.DALRelationImpl;
import dalobject.DALRoomImpl;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import utils.DefaultPort;
import utils.RemoteFactory;

/**
 *
 * @author salm
 */
public class DALFactoryImpl implements DALFactory
{
    private Connection connection;
    private DALAccount dalAccount;
    private DALQuerry dalQuerry;
    private DALMessage dalMessage;
    private DALRoom dalRoom;
    private DALRelation dalRelation;
    
    public DALFactoryImpl(Connection conn) throws RemoteException
    {
        this.connection = conn;
    }
    
    private void createDALAccount() throws RemoteException
    {
        DALAccount dalAcc = new DALAccountImpl(connection);
        this.dalAccount = (DALAccount) UnicastRemoteObject.exportObject(dalAcc,  DefaultPort.getInstance().getDALPort());
    }
    
    private void createDALQuerry() throws RemoteException
    {
        DALQuerry dalQrr =   new DALQuerryImpl(connection);
        this.dalQuerry      =   (DALQuerry) UnicastRemoteObject.exportObject(dalQrr, DefaultPort.getInstance().getDALPort());
    }
    
    private void createRelation() throws RemoteException {
        DALRelation dalRela =   new DALRelationImpl(connection);
        this.dalRelation      =   (DALRelation) UnicastRemoteObject.exportObject(dalRela, DefaultPort.getInstance().getDALPort());
    }
        
    private void createRoom() throws RemoteException {
        //DALRoom dalroom =   new DALRoomImpl(connection);
        //this.dalRoom      =   (DALRoom) UnicastRemoteObject.exportObject(dalroom, DefaultPort.getInstance().getDALPort());
    }
    
    @Override
    public DALAccount getAccount() throws RemoteException
    { 
        System.out.println("Requeted");
        if (this.dalAccount == null)
        {
            this.createDALAccount();
        }
        return this.dalAccount;
    }

    // singleton
    @Override
    public DALQuerry getQuerry() throws RemoteException {
        System.out.println("Request get querry");
        if( this.dalQuerry == null )
        {
            this.createDALQuerry();
        }
        return this.dalQuerry;
    }

    @Override
    public DALRelation getRelation() throws RemoteException {
        System.out.println("Request get querry");
        if( this.dalRelation == null )
        {
            this.createRelation();
        }
        return this.dalRelation;
    }

    @Override
    public DALRoom getRoom() throws RemoteException {
        if (this.dalRoom == null)
        {
            this.dalRoom = RemoteFactory.<DALRoom>createRemote(new DALRoomImpl(this.connection),
                    DefaultPort.getInstance().getDALPort());
        }
        
        return this.dalRoom;
    }

    @Override
    public DALMessage getMessage() throws RemoteException
    {
        if (this.dalMessage == null)
        {
            this.dalMessage = RemoteFactory.<DALMessage>createRemote(new DALMessageImpl(connection),
                    DefaultPort.getInstance().getDALPort());
        }
        
        return this.dalMessage;
    }
}
